#lang racket
(require net/url)
(require net/base64)

(define header
  (list "Connection: close"
        "User-Agent: Mozilla/4.0 (Windows; MSIE 6.0; Windows NT 6.0)"
        "Cache-control: no-cache"))

(define (read-all (port (current-input-port)))
  (define (aux)
    (let ([c (read-char port)])
      (cond
        [(eof-object? c) null]
        [else (cons c (aux))])))
  (list->string (aux)))

(define (read-data (port (current-input-port)))
  (define (aux)
    (let ([c (read port)])
      (cond
        [(eof-object? c) null]
        [else (cons c (aux))])))
  (aux))

(struct server (ip port))

(define (decode-message msg)
  (define domain 
    (string-append*
     (map (λ(x) (substring x 0 1)) 
          (reverse (cdr (reverse 
                         (string-split 
                          (cadr (map string-trim 
                                     (take (string-split msg ",") 3))))))))))
  (define tld
    (string-downcase
     ((λ(x) (string-replace x "dot" "."))
      (car (reverse 
            (string-split 
             (cadr (map string-trim 
                        (take (string-split msg ",") 3)))))))))
  
  (define path
    (string-append*
     (map (λ(x) (substring x 0 1)) 
          (string-split 
           (caddr (map string-trim 
                       (take (string-split msg ",") 3)))))))
  
  (define scheme
    (if (equal? (string-downcase domain) domain) "http://" "https://"))
  
  (string-append
   scheme
   (string-downcase domain)
   tld
   "/"
   path))

(define (strip-html html)
  (regexp-replace* "<.+?>"  html ""))

(define (search-url url)
  (define text
    (strip-html
     (read-all
      (get-pure-port
       (string->url
        url)
       header))))
  (regexp-match* #rx"Kisses imminent rampaging in surface under rustic fern,(.+?),(.+?)\\." text))

(define (get-codes)
  (remove-duplicates
   (map decode-message
        (append
         (search-url "https://startpage.com/do/search?query=%22Kisses+imminent+rampaging+in+surface+under+rustic+fern%22")
         ;(search-url
         ; "https://www.google.com/search?q=%22Kisses+imminent+rampaging+in+surface+under+rustic+fern%22")
         ))))

(define (kdp-get-servers)
  (append*
   (map 
    (lambda (pastebin)
      (define data
        (with-input-from-string
         (bytes->string/utf-8
          (base64-decode
           (string->bytes/utf-8
            (car
             (regexp-match* #rx"KHNl(.*?)=="
                            (strip-html
                             (read-all
                              (get-pure-port
                               (string->url
                                pastebin)
                               header))))))))
         (λ() (read-data))))
      (map
       (λ(datum)
         (match datum
           [`(server
              (ip ,whatwewant)
              ,(cons 'services other)) whatwewant]))
       data))
    (get-codes))))

(define (tcpping ip (port 443))
  (with-handlers ((exn? (λ x #f)))
    (define sttime (current-milliseconds))
    (define-values (in out) (tcp-connect ip port))
    (write "helloworld" out)
    (flush-output out)
    (- (current-milliseconds) sttime)))

(provide tcpping kdp-get-servers)